#include"TcpServer.hpp"

void Use(char* ch[])
{
    std::cout << ch[0] << " server ip server port" << std::endl;
}


int main(int argc,char* argv[])
{
    if(argc != 3)
    {
        Use(argv);
        exit(1);
    }

    std::string _ip = argv[1];
    uint16_t _port = std::stoi(argv[2]);
    
    int _sockfd = socket(AF_INET,SOCK_STREAM,0);

    Inet_Addr _server(_port,_ip);

    int n = connect(_sockfd,_server.Addr(),_server.Size());
    if(n < 0)
    {
        perror("connect");
        exit(1);
    }


    while(1)
    {
        std::string _line;
        std::getline(std::cin,_line);

        char buffer[1024] = {0};


        ssize_t n = write(_sockfd,_line.c_str(),_line.size());
        ssize_t ret = read(_sockfd,buffer,sizeof(buffer) - 1);
        if(n > 0)
        {
            buffer[ret] = 0;
            std::cout << buffer << std::endl;            
        }
    }


    return 0;
}